//
//  MyPlanVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/1/19.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "MyPlanVC.h"
#import "TransactionRecordsCell.h"
#import "PlanVM.h"
#import "MJRefresh.h"
#import "Home_TypeView.h"
#import "HomePageVM.h"
#import "JoinQuizDetailVC.h"
@interface MyPlanVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *dataArr;
@property (nonatomic ,strong) UIView        *topView;
@property (nonatomic ,strong) Home_TypeView *home_TypeView;
@property (nonatomic ,strong) NSMutableArray *totalArr;
@end

@implementation MyPlanVC{
    ///开奖状态 0=全部 1=未开奖 2=已开奖 默认=0
    NSInteger   winState;
    NSInteger   page;
    UIView *lineView;
    UIButton *btnAll;
    UIButton *btnHasLottery;
    UIButton *btnNoLottery;
    NSInteger   index;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"我的计划";
    UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithTitle:@"筛选" style:UIBarButtonItemStylePlain target:self action:@selector(onclickScreening)];
    self.navigationItem.rightBarButtonItem=item;
    [self.tableView.header beginRefreshing];
    [self headView];
}

- (void)backClick{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)headView{
    WS(weakSelf);
    UIView *topView=[[UIView alloc]init];
    topView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(weakSelf.view);
        make.height.mas_equalTo(@39);
    }];
    
    btnAll=[self createBtnWithTitle:@"全部"];
    btnAll.tag=0;
    [topView addSubview:btnAll];
    [btnAll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(topView);
        make.width.equalTo(topView).with.multipliedBy(1/3.0);
        make.height.mas_equalTo(@39);
    }];
    
    btnHasLottery=[self createBtnWithTitle:@"已开奖"];
    btnHasLottery.tag=2;
    [topView addSubview:btnHasLottery];
    [btnHasLottery mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView);
        make.centerX.equalTo(topView);
        make.width.equalTo(topView).with.multipliedBy(1/3.0);
        make.height.mas_equalTo(@39);
    }];
    
    btnNoLottery=[self createBtnWithTitle:@"未开奖"];
    btnNoLottery.tag=1;
    [topView addSubview:btnNoLottery];
    [btnNoLottery mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView);
        make.right.equalTo(topView);
        make.width.equalTo(topView).with.multipliedBy(1/3.0);
        make.height.mas_equalTo(@39);
    }];
    
    lineView=[[UIView alloc]init];
    lineView.backgroundColor=NAVCOLOR;
    [topView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(btnAll);
        make.bottom.equalTo(topView);
        make.size.mas_equalTo(CGSizeMake(44, 2));
    }];
}

- (UIButton *)createBtnWithTitle:(NSString *)title{
    UIButton *btn=[[UIButton alloc]init];
    [btn addTarget:self action:@selector(onclickTop:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:DEFAULTTEXTGDARKRAYCOLOR forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    return btn;
}


- (void)onclickTop:(UIButton *)sender{
    winState=sender.tag;
    [self.tableView.header beginRefreshing];
    if (sender.tag==0) {
        [lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(sender);
        }];
    }else if (sender.tag==2){
        [lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(btnAll).with.offset(SCREEN_WIDTH/3.0);
        }];
    }else if (sender.tag==1){
        [lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(btnAll).with.offset(SCREEN_WIDTH/3.0*2);
        }];
    }
}


- (void)getData{
    WS(weakSelf);
    [[PlanVM manage] getMyPlanWithUid:[UserInfoModel share].Id andLtype:self.lType andWinState:winState andPageIndex:page Success:^(id returnValue) {
        NSArray *arr=returnValue;
        if (arr.count<[defaultPageSize integerValue]) {
            [weakSelf.tableView.footer noticeNoMoreData];
        }else{
            [weakSelf.tableView.footer endRefreshing];
        }
        [weakSelf.tableView.header endRefreshing];
        page+=1;
        [weakSelf.dataArr addObjectsFromArray:returnValue];
        [weakSelf.tableView reloadData];
    } andFail:^(NSString *errorMessage, NSError *error) {
        [weakSelf.tableView.footer endRefreshing];
        [weakSelf.tableView.header endRefreshing];
    }];
}

///筛选
- (void)onclickScreening{
    WS(weakSelf);
    if (!self.totalArr) {
        [[PlanVM manage] getLotteryListOfMyPlanWithSuccess:^(id returnValue) {
            weakSelf.totalArr=returnValue;
            [weakSelf onclickScreening];
        } andFail:^(NSString *errorMessage, NSError *error) {
            
        }];
    }else{
        if (self.home_TypeView) {
            [self.home_TypeView remove];
            self.home_TypeView=nil;
            return;
        }
        self.home_TypeView=[[Home_TypeView alloc]init];
        [self.home_TypeView setNOTitleArr:self.totalArr andIndex:index andSelectBlock:^(NSInteger row) {
            index=row;
            PlanModel *model=weakSelf.totalArr[row];
            weakSelf.lType=model.lType;
            weakSelf.home_TypeView=nil;
            [weakSelf.tableView.header beginRefreshing];
        }];
        [self.view addSubview:self.home_TypeView];
        [self.home_TypeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.bottom.equalTo(weakSelf.view);
        }];
    }
}

#pragma mark -- UITableViewDelegate ,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TransactionRecordsCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TransactionRecordsCell" forIndexPath:indexPath];
    cell.userId=[UserModel userModel].UserId;
    cell.planModel=self.dataArr[indexPath.row];
    WS(weakSelf);
    cell.editBlock = ^(PlanModel *model) {
        [weakSelf editPlan:model];
    };
    return cell;
}
///编辑计划
- (void)editPlan:(PlanModel *)model{
    JoinQuizDetailVC *vc=[[JoinQuizDetailVC alloc]init];
    vc.title=model.LTypeName;
    vc.lType=model.lType;
    [self.navigationController pushViewController:vc animated:YES];
}
///查看成绩
- (void)lookResults:(PlanModel *)model{
    JoinQuizDetailVC *vc=[[JoinQuizDetailVC alloc]init];
    vc.title=model.LTypeName;
    vc.lType=model.lType;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- 懒加载



- (UITableView *)tableView{
    if (!_tableView) {
        WS(weakSelf);
        _tableView=[[UITableView alloc]init];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=DEFAULTBGCOLOR;
        [_tableView registerClass:[TransactionRecordsCell class] forCellReuseIdentifier:@"TransactionRecordsCell"];
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            page=1;
            [weakSelf.dataArr removeAllObjects];
            [weakSelf.tableView reloadData];
            [weakSelf getData];
        }];
        _tableView.footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf getData];
        }];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf.view).with.
            insets(UIEdgeInsetsMake(44, 0, 0, 0));
        }];
    }
    return _tableView;
}

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[[NSMutableArray alloc]init];
    }
    return _dataArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
